﻿#region Imports

using System;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

#endregion

namespace IEC.ColumnOrdering.SP2007.Application.UI.Controls.UserControls
{

    /// <summary>
    /// Provides UI Management functionality.
    /// </summary>
    public partial class UIManager : UserControl
    {

        //// TODO: add tag prefix (http://msdn.microsoft.com/en-us/library/system.web.ui.tagprefixattribute.aspx)

        #region Classes

        /// <summary>
        /// The supported browser product.
        /// </summary>
        private static class SupportedBrowserProduct
        {

            #region Constants

            /// <summary>
            /// Represents Microsoft Internet Explorer.
            /// </summary>
            public const string InternetExplorer = "IE";

            #endregion

        }

        /// <summary>
        /// The supported browser version.
        /// </summary>
        private static class SupportedBrowserVersion
        {

            #region Constants

            /// <summary>
            /// Represents the minimum supported version of Microsoft Internet Explorer.
            /// </summary>
            public const int InternetExplorerMinimum = 6;

            #endregion

        }

        /// <summary>
        /// The header used for transition effects.
        /// </summary>
        private static class TransitionHeader
        {

            /// <summary>
            /// The key of the transition header.
            /// </summary>
            public static class Key
            {

                #region Constants

                /// <summary>
                /// Represents the key for transitions on Page Enter events.
                /// </summary>
                public const string PageEnter = "Page-Enter";

                /// <summary>
                /// Represents the key for transitions on Page Exit events.
                /// </summary>
                public const string PageExit = "Page-Exit";

                #endregion

            }

            /// <summary>
            /// The value of the transition header.
            /// </summary>
            public static class Value
            {

                #region Constants

                /// <summary>
                /// Represents the immediate fade transition effect.
                /// </summary>
                public const string FadeImmediate = "progid:DXImageTransform.Microsoft.Fade(duration=0)";

                #endregion

            }

        }

        #endregion

        #region Methods - Instance Member

        #region Methods - Instance Member - UIManager Members

        #region Methods - Instance Member - UIManager Members - (event handlers)

        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">
        /// The source of the event.
        /// </param>
        /// <param name="e">
        /// The <see cref="System.EventArgs"/> instance containing the event data.
        /// </param>
        protected void Page_Load(object sender, EventArgs e)
        {
            this.SetupScrollPosition();
            this.DisableVisualRefresh();
        }

        #endregion

        #region Methods - Instance Member - UIManager Members - (helpers)

        /// <summary>
        /// Sets up the scroll position.
        /// </summary>
        private void SetupScrollPosition()
        {
            // TODO: verify if a this.IsPostBack is needed
            this.Page.MaintainScrollPositionOnPostBack = true;
        }

        /// <summary>
        /// Disables the visual refresh (flicker).
        /// </summary>
        private void DisableVisualRefresh()
        {
            HttpBrowserCapabilities capabilities = this.Request.Browser;

            if ((capabilities.Browser == SupportedBrowserProduct.InternetExplorer) &&
                (capabilities.MajorVersion > SupportedBrowserVersion.InternetExplorerMinimum))
            {
                HtmlHead header = this.Page.Header;

                if (header != null)
                {
                    HtmlMeta meta = new HtmlMeta();

                    // TODO: test with Page-Enter and Page-Exit; the latter will apply even
                    // when browsing away from the site...
                    meta.HttpEquiv = TransitionHeader.Key.PageEnter;
                    meta.Content = TransitionHeader.Value.FadeImmediate;

                    // TODO: verify if needed; 
                    // this is a guard against multiple instances of this meta tag being setup in a page
                    if (!(header.Controls.Contains(meta)))
                    {
                        header.Controls.Add(meta);
                    }
                }
            }
        }

        #endregion

        #endregion

        #endregion

    }

}